package org.example.controller;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.Stock;
import org.example.domain.TakePosition;
import org.example.service.StockService;
import org.example.service.TakePositionService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/stock")
@Api(tags = "股票接口")
public class StockController {
    @Autowired
    private StockService stockService;
    @Autowired
    private TakePositionService takePositionService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/list")
    public Result list(@RequestBody Stock stock, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Stock> list = stockService.listall(stock);
        PageInfo<Stock> stockPageInfo = new PageInfo<>(list);
        return Result.success(stockPageInfo);
    }

    @PostConstruct
    public void initList() {
        List<Stock> list = stockService.list();
        if (!redisTemplate.hasKey("StockList")) {
            redisTemplate.opsForList().rightPushAll("StockList", list);
        }
    }

    @RequestMapping("/list1")
    public Result list1(Integer pageNum, Integer pageSize) {
        List<Stock> list = null;

        if (!redisTemplate.hasKey("StockList")) {
            list = stockService.list();
            System.out.println("redis里没有数据，重新添加/n");
            redisTemplate.opsForList().rightPushAll("StockList", list);
        }
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize - 1;
        list = redisTemplate.opsForList().range("StockList", start, end);
        Long totalCount = redisTemplate.opsForList().size("StockList");
        Map<Object, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", totalCount);
        return Result.success(map);
    }


    @ApiOperation(value = "买入股票", httpMethod = "POST", notes = "用来创建一个新的对象")
    @RequestMapping("buy")
    public Result buy(@RequestBody TakePosition takePosition, HttpServletRequest request) {
        String ip = request.getRequestURI();
        String s = DigestUtil.md5Hex(takePosition + ip);
        if (!redisTemplate.opsForValue().setIfAbsent(s, "1111", 5, TimeUnit.SECONDS)) {
            return Result.fail("请勿重复提交");
        }
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("id");
        String save = takePositionService.buy(takePosition, userId);
        //放回前端
        return save.equals("购买成功") ? Result.success("买入成功") : Result.fail(save);
    }

    @RequestMapping("/getNewmoney")
    private Result getNewmoney(Integer id) {
        Stock byId = stockService.getById(id);
        return Result.success(byId);
    }
}